package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.CarSeriesAdapter;
import com.wqtz.chechai.adapter.CarSeriesDetailAdapter;
import com.wqtz.chechai.api.Constans;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.CarSeriesDetailBean;
import com.wqtz.chechai.bean.SeriesBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.JsonUtils;
import com.wqtz.chechai.utils.NetworkUtils;
import com.wqtz.chechai.utils.SPUtils;
import com.wqtz.chechai.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.R.attr.name;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/29 11:04
 */

public class CarSeriesDetailActivity extends BaseActivity implements NetworkUtils.GetJson, CarSeriesDetailAdapter.BigOnClick {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.iv_car_img)
	ImageView mIvCarImg;
	@BindView(R.id.tv_carbrand)
	TextView mTvCarbrand;
	@BindView(R.id.iv_del_return)
	ImageView mIvDelReturn;
	@BindView(R.id.ll_car_model)
	LinearLayout mLlCarModel;
	@BindView(R.id.iv_car_series)
	ImageView mIvCarSeries;
	@BindView(R.id.tv_carseries)
	TextView mTvCarseries;
	@BindView(R.id.iv_del_return_brand)
	ImageView mIvDelReturnBrand;
	@BindView(R.id.ll_car_detail)
	LinearLayout mLlCarDetail;
	@BindView(R.id.rv_carSeries)
	RecyclerView mRvCarSeries;
	private String url;
	private String json;
	private String xmid;
	private String name;
	private CarSeriesDetailBean seriesBean;
	CarSeriesDetailAdapter mCarSeriesDetailAdapter;
	List<CarSeriesDetailBean.ResultBean.DataBean> list;
	private String carseriesName;
	private String carBrand;

	@Override
	public void init() {
		ButterKnife.bind(this);
		Intent intent = getIntent();
		xmid = intent.getStringExtra("xmid");
		name = intent.getStringExtra("name");
		carBrand = intent.getStringExtra("carBrand");
		carseriesName=intent.getStringExtra("carseriesName");
		Logger.e(xmid);
		url = String.format(Constans.USER_CARLIST, xmid);
		list = new ArrayList<>();
		NetworkUtils.doHttpAsync(url, this, this);

	}

	@Override
	public void loadData() {
		mTvCarbrand.setText(carBrand);
		mTvCarseries.setText(carseriesName);
	}

	@Override
	public int setContentViews() {
		return R.layout.activity_carseriesdetail;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}



	@OnClick({R.id.iv_back, R.id.iv_del_return, R.id.iv_del_return_brand})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.iv_del_return:
				Intent intent = new Intent(this,CarBrandSelectActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.iv_del_return_brand:
				finish();
				break;
		}
	}

	@Override
	public void getJsonCallBack(String result, String request_url) {
		if (url.equals(request_url) && result != null)
			seriesBean = new CarSeriesDetailBean();
		Logger.e(result);
		seriesBean = JsonUtils.getCarSeriesDetail(result);
		list.addAll(seriesBean.getResult().getData()) ;
		mCarSeriesDetailAdapter = new CarSeriesDetailAdapter(R.layout.item_carseries_first,list,this);
		mRvCarSeries.setLayoutManager(new LinearLayoutManager(this));
		mRvCarSeries.setAdapter(mCarSeriesDetailAdapter);
		mCarSeriesDetailAdapter.setBigClick(this);
	}

	@Override
	public void bigClick(int positon, List<CarSeriesDetailBean.ResultBean.DataBean.ChexingListBean> list) {
		//ToastUtils.show(this,list.get(positon).getCxname(),1000);
		Intent intent = new Intent();
		intent.putExtra("cxname",list.get(positon).getCxname());
		intent.putExtra("id",list.get(positon).getId());
		intent.putExtra("price",list.get(positon).getPrice());
		setResult(RESULT_OK,intent);
		this.finish();

	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
